import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export const store = new Vuex.Store({
  actions: {
     setUser({commit},user){
       commit("userCach",user)
     }
  },
  mutations: {
    userCach(state, user) {
      if (user) {
        state.userName = user.userName
        state.headUrl = user.headUrl
        state.jurisdiction = user.jurisdiction
        state.isLogin = true
        var v = user.jurisdiction > 1;
        state.isShow = v;
        localStorage.setItem('userName', user.userName)
        localStorage.setItem('headUrl', user.headUrl)
        localStorage.setItem('jurisdiction', user.jurisdiction)
        localStorage.setItem('isLogin', true)
        localStorage.setItem('isShow', v)
      } else {
        state.userName = ''
        state.headUrl = ''
        state.jurisdiction = ''
        state.isLogin = ''//1是未登陆 2是登陆
        state.isShow=''
        localStorage.removeItem('userName')
        localStorage.removeItem('headUrl')
        localStorage.removeItem('jurisdiction')
        localStorage.removeItem('isLogin')
        localStorage.removeItem('isShow')
      }
    }
  },
  state: {
    userName: localStorage.getItem('userName') || '',
    headUrl: localStorage.getItem('headUrl') || '',
    jurisdiction:  localStorage.getItem('jurisdiction') || 0,
    isLogin: localStorage.getItem('isLogin') || true,
    isShow: localStorage.getItem('isShow') || false,
  },
  getters: {
    userName: state => state.userName,
    headUrl: state => state.headUrl,
    jurisdiction: state => state.jurisdiction,
    isLogin: state => state.isLogin,
    isShow:state => state.isShow,
  },
})
